package socketmvc.core.packets.basic;

import socketmvc.core.packets.basic.impl.JsonTextBody;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

public abstract non-sealed class JsonBasicPacket implements IPacket {
    /**
     * 文本内容
     */
    protected JsonTextBody textBody;
    /**
     * 二进制内容
     */
    protected byte[] binBody;

    /**
     * 业务消息指令
     */
    protected String command;

    /**
     * 客户端传递过来的回调函数名称
     */
    protected String clientCallbackName;

    protected JsonBasicPacket(){}


    public void setBinBody(byte[] body) {
        this.binBody = body;
    }

    public void setTextBody(JsonTextBody textBody) {
        this.textBody = textBody;
    }

    @Override
    public String getTextBodyAsStr() {
        return textBody.toString();
    }

    @Override
    public JsonTextBody getTextBodyAsObj() {
        if (textBody == null){
            textBody = new JsonTextBody();
        }
        return textBody;
    }

    @Override
    public InputStream getBinBodyAsStream() {
        return new ByteArrayInputStream(getBinBodyAsBytes());
    }

    @Override
    public byte[] getBinBodyAsBytes() {
        return binBody;
    }

    /**
     * 获取包内指定的业务路径
     * @return str
     */
    @Override
    public String getCommand(){
        if (command != null && !command.isBlank()){
            return command;
        }
        JsonTextBody textBodyAsJSON = this.getTextBodyAsObj();
        if (command == null && textBodyAsJSON != null){
            command = textBodyAsJSON.getCommand();
        }
        return command;
    }

    public void setCommand(String cmd){
        this.command = cmd;
        JsonTextBody textBodyAsJSON = this.getTextBodyAsObj();
        if (textBodyAsJSON == null){
            textBodyAsJSON = new JsonTextBody();
        }
        textBodyAsJSON.setCommand(cmd);
    }

    @Override
    public String getResponseKey(){
        if (clientCallbackName != null && !clientCallbackName.isBlank()){
            return clientCallbackName;
        }
        JsonTextBody textBodyAsJSON = this.getTextBodyAsObj();
        if (clientCallbackName == null && textBodyAsJSON != null){
            clientCallbackName = textBodyAsJSON.getResponseKey();
        }
        return clientCallbackName;
    }

    /**
     * 获取客户端回调
     *
     * @param echo
     * @return 客户端回调名称
     */
    @Override
    public void setResponseKey(String echo) {
        JsonTextBody textBodyAsJSON = this.getTextBodyAsObj();
        textBodyAsJSON.setResponseKey(echo);
    }

    /**
     * 获取本包是否是客户端回复过来的回调包
     *
     * @return true代表是回复包
     */
    @Override
    public boolean isResponsePkt() {
        JsonTextBody textBodyAsJSON = this.getTextBodyAsObj();
        if (textBodyAsJSON != null){
            return textBodyAsJSON.isResponse();
        }
        return false;
    }

    /**
     * 设置本包是回复包
     *
     * @param isResponsePkt true代表是回复包
     */
    @Override
    public void setResponsePkt(boolean isResponsePkt) {
        JsonTextBody textBodyAsJSON = this.getTextBodyAsObj();
        if (textBodyAsJSON != null){
            textBodyAsJSON.setResponse(isResponsePkt);
        }
    }

    /**
     * 设置属性
     *
     * @param key k
     * @param val v
     */
    @Override
    public void setAttr(String key, Object val) {
        JsonTextBody textBodyAsJSON = this.getTextBodyAsObj();
        textBodyAsJSON.setHeader(key,val.toString());
    }

    /**
     * 获取属性
     *
     * @param key   k
     * @param clazz class
     * @return v
     */
    @Override
    public <T> T getAttr(String key, Class<T> clazz) {
        return this.getTextBodyAsObj().getObject(key,clazz);
    }
}
